package service.impl;

import BBS.bean.UserInfo;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import service.UserService;

import javax.servlet.ServletContext;

public class UserServiceImpl implements UserService {
   private UserMapper um;
   private SqlSession session;

    public UserServiceImpl(ServletContext application) {
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SqlSessionFactory");
        session = ssf.openSession();
        um = session.getMapper(UserMapper.class);
    }

    @Override
    public UserInfo findUserByUser(UserInfo userInfo) {
        UserInfo ui = um.findUserByUser(userInfo);
        session.close();
        return ui;
    }

    @Override
    public void saveUser(UserInfo ui) {
            um.saveUser(ui);
            session.commit();
            session.close();
    }

    @Override
    public Integer checkUserName(UserInfo ui) {
        int i  = um.checkUserName(ui);
        return i;
    }

}
